import {createRouter, createWebHashHistory} from 'vue-router'
import Cookies from "js-cookie";
import axios from "axios";

const routes = [
    {
        path: '/jcdl',
        name: 'jcdl',
        component: () => import('../jcdl/CommonPage.vue'),
        children: [
            {
                path: "",
                component: () => import('../jcdl/ServiceEntrance.vue')
            }
        ]
    },
    {
        path:'/aiBusiness0',
        name:'aiBusiness0',
        component:()=>import('../jcdl/AiBusiness0.vue')
    },
    {
        path: '/aiBusiness1',
        name: 'aiBusiness1',
        component: () => import('../jcdl/AiBusiness1.vue'),
        beforeEnter(to, from, next) {
            to.query.toPath = to.path
            if( to.query.login === 'login') {
                router.push({
                    path: '/bindPhone',
                    name: 'bindPhone',
                    query: to.query
                })
            }else if(to.query.uuid !== null && to.query.uuid !== undefined){
                const url = `https://www.pengzhou.org.cn/api/public/getUserToken`;
                axios.get(url,{},{
                    headers: {
                        'Content-Type': 'multipart/form-data' // 不需要设置Content-Type，axios会自动设置，但如果需要自定义，可以添加此行。默认情况下不需要设置。
                    }}).then(response => {
                    if (response.data && response.data.token) {
                        Cookies.set('token', response.data.token);
                        axios.defaults.headers.common['token'] = response.data.token;
                        next();
                    }else{
                        Cookies.delete('token')
                        router.push({
                            path: '/jcdl',
                            name: 'jcdl'
                        })
                    }}).catch(error => {
                });
            }else if(Cookies.get('token') != null){
                next();
            }else {
                next()
            }
        }
    },
    {
        path: '/result',
        name: 'result',
        component: () => import('../jcdl/AppraiseQJ.vue')
    },
    {
        path: '/download',
        name: 'download',
        component: () => import('../jcdl/Download.vue')
    },
    {
        path: '/allBusinessList',
        name: 'allBusinessList',
        component: () => import('../jcdl/AllBusinessList.vue')
    },
    {
        path: '/businessDetails',
        name: 'businessDetails',
        component: () => import('../jcdl/BusinessDetails.vue')
    },
    {
        path: '/bindPhone',
        name: 'bindPhone',
        component: () => import('../jcdl/BindPhone.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

export default router
